package com.example.day6.View;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.day6.Model.Bean;
import com.example.day6.R;

import java.util.ArrayList;
import java.util.List;

class RAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<Bean.DataBean.DatasBean> list;

    public RAdapter(Context context, ArrayList<Bean.DataBean.DatasBean> list) {
        this.context = context;
        this.list = list;
    }

    public void addData(List<Bean.DataBean.DatasBean> datas) {
        list.addAll(datas);
        notifyDataSetChanged();
    }

    class ViewHolder1 extends RecyclerView.ViewHolder {

        private ImageView pic;
        private TextView desc;

        public ViewHolder1(@NonNull View itemView) {
            super(itemView);

            pic = itemView.findViewById(R.id.a_fragment_1_pic);
            desc = itemView.findViewById(R.id.a_fragment_1_desc);
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {

        private TextView desc;
        private ImageView pic;

        public ViewHolder2(@NonNull View itemView) {
            super(itemView);

            desc = itemView.findViewById(R.id.a_fragment_2_desc);
            pic = itemView.findViewById(R.id.a_fragment_2_pic);

        }
    }

    @Override
    public int getItemViewType(int position) {

        if (position % 2 == 0) {
            return 1;
        } else {
            return 2;
        }

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        if (i == 1) {
            View view = LayoutInflater.from(context).inflate(R.layout.a_fragment_1, null);
            return new ViewHolder1(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.a_fragment_2, null);
            return new ViewHolder2(view);
        }


    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        int viewtype = getItemViewType(i);

        if (viewtype == 1) {
            ViewHolder1 viewHolder1 = (ViewHolder1) viewHolder;
            viewHolder1.desc.setText(list.get(i).getDesc());
            Glide.with(context).load(list.get(i).getEnvelopePic()).into(viewHolder1.pic);

        } else {
            ViewHolder2 viewHolder2 = (ViewHolder2) viewHolder;
            viewHolder2.desc.setText(list.get(i).getDesc());
            Glide.with(context).load(list.get(i).getEnvelopePic()).into(viewHolder2.pic);

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
